import pygame
from src.game.tetris import Tetris
from src.game.constants import *
from src.utils.colors import BLACK
from src.utils.config import Config
from src.game.sound import SoundManager

# 添加按键重复延迟和间隔
KEY_REPEAT_DELAY = 200  # 毫秒
KEY_REPEAT_INTERVAL = 100  # 毫秒

def main():
    # 初始化 Pygame
    pygame.init()
    pygame.mixer.init()
    
    # 加载配置
    config = Config()
    
    # 创建游戏窗口
    flags = pygame.FULLSCREEN if config.config['fullscreen'] else 0
    screen = pygame.display.set_mode((WINDOW_WIDTH, WINDOW_HEIGHT), flags)
    pygame.display.set_caption('俄罗斯方块')
    
    # 初始化游戏组件
    sound_manager = SoundManager(config)
    game = Tetris(config, sound_manager)
    clock = pygame.time.Clock()
    
    # 初始化按键重复
    pygame.key.set_repeat(KEY_REPEAT_DELAY, KEY_REPEAT_INTERVAL)
    
    # 游戏主循环
    running = True
    while running:
        # 处理输入
        running = handle_input(game, config, sound_manager)
        
        # 更新游戏状态
        update_game(game)
        
        # 渲染画面
        render(screen, game)
        
        # 控制帧率
        clock.tick(FPS)
    
    # 退出前保存
    config.save_config()
    pygame.quit()

def handle_input(game: Tetris, config: Config, sound_manager: SoundManager) -> bool:
    """处理用户输入"""
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            return False
            
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_ESCAPE:
                return False
                
            # 处理游戏开始/重启
            if event.key == pygame.K_SPACE:
                handle_space_key(game, sound_manager)
                return True
                
            # 处理暂停键 (游戏开始且未结束时可用)
            # 同时支持大小写 P 键和中文输入状态
            if event.key in [pygame.K_p, pygame.K_p - 32] or event.unicode.lower() == 'p':
                if game.game_started and not game.game_over:
                    game.toggle_pause()
                    sound_manager.play('menu')  # 添加暂停音效反馈
                return True
                
            # 只在游戏进行且未暂停时处理其他按键
            if not game.paused and game.is_playing():
                handle_game_control(event.key, game, config, sound_manager)
                
    return True

def handle_space_key(game: Tetris, sound_manager: SoundManager):
    """处理空格键"""
    if not game.game_started:
        game.start()
    elif game.game_over:
        game.restart()
    elif not game.paused:
        game.hard_drop()

def handle_game_control(key: int, game: Tetris, config: Config, sound_manager: SoundManager):
    """处理游戏控制键"""
    if key == config.get_key('left'):
        game.move(-1, 0)
    elif key == config.get_key('right'):
        game.move(1, 0)
    elif key == config.get_key('down'):
        game.move(0, 1)
    elif key == config.get_key('rotate'):
        game.rotate()
        sound_manager.play('rotate')

def update_game(game: Tetris):
    """更新游戏状态"""
    # 只在游戏进行且未暂停时更新
    if not game.paused and game.is_playing():
        current_time = pygame.time.get_ticks()
        if current_time - game.last_fall > game.fall_speed:
            game.move(0, 1)
            game.last_fall = current_time

def render(screen: pygame.Surface, game: Tetris):
    """渲染游戏画面"""
    screen.fill(BLACK)
    game.draw(screen)
    pygame.display.flip()

if __name__ == '__main__':
    main() 